<template>
  <div class="editor-header">
    <div class="left">
      <Popover id="ppt-des-1" trigger="click" placement="bottom-start" v-model:value="mainMenuVisible">
        <template #content>
          <FileInput accept=".pptist" @change="files => {
            importSpecificFile(files)
            mainMenuVisible = false
          }">
            <PopoverMenuItem>导入 pptist 文件</PopoverMenuItem>
          </FileInput>
          <FileInput accept="application/vnd.openxmlformats-officedocument.presentationml.presentation" @change="files => {
            importPPTXFile(files)
            mainMenuVisible = false
          }">
            <PopoverMenuItem>导入 pptx 文件</PopoverMenuItem>
          </FileInput>
          <PopoverMenuItem @click="setDialogForExport('pptx')">导出文件</PopoverMenuItem>
          <PopoverMenuItem @click="resetSlides(); mainMenuVisible = false">重置幻灯片</PopoverMenuItem>
          <!-- <PopoverMenuItem @click="goLink('https://github.com/pipipi-pikachu/PPTist/issues')">意见反馈</PopoverMenuItem>
          <PopoverMenuItem @click="goLink('https://github.com/pipipi-pikachu/PPTist/blob/master/doc/Q&A.md')">常见问题
          </PopoverMenuItem> -->
          <PopoverMenuItem @click="mainMenuVisible = false; hotkeyDrawerVisible = true">快捷键</PopoverMenuItem>
        </template>
        <div class="menu-item" data-class="ppt-first">
          <IconHamburgerButton class="icon" />
        </div>
      </Popover>

      <div class="title">
        <Input class="title-input" ref="titleInputRef" v-model:value="titleValue" @blur="handleUpdateTitle()"
          v-if="editingTitle"></Input>
        <div class="title-text" @click="startEditTitle()" :title="title" v-else>{{ title }}</div>
      </div>
      <!-- <div class="help" @click="helpVisible = true">
        <IconInfo class="icon" />
        使用帮助
      </div> -->
    </div>

    <div class="right">
      <!-- <div class="group-menu-item">
        <div class="menu-item" v-tooltip="'幻灯片放映'" @click="enterScreening()">
          <IconPpt class="icon" />
        </div>
        <Popover trigger="click" center>
          <template #content>
            <PopoverMenuItem @click="enterScreeningFromStart()">从头开始</PopoverMenuItem>
            <PopoverMenuItem @click="enterScreening()">从当前页开始</PopoverMenuItem>
          </template>
          <div class="arrow-btn">
            <IconDown class="arrow" />
          </div>
        </Popover>
      </div>
      <div class="menu-item" v-tooltip="'关联报价单数据'" @click="linkDialogForQuote">
        <IconAfferent class="icon" />
      </div>
        <div class="menu-item" v-tooltip="'导出'" @click="setDialogForExport('pptx')">
          <IconDownload class="icon" />
        </div>
        <div class="menu-item" id="ppt-des-5" v-tooltip="'保存模板到模板库'" @click="saveModule">
          <IconMemoryCard class="icon" />
        </div> -->
      <!-- <a class="github-link" v-tooltip="'Copyright © 2020-PRESENT pipipi-pikachu'" href="https://github.com/pipipi-pikachu/PPTist" target="_blank">
        <div class="menu-item"><IconGithub class="icon" /></div>
      </a> -->
    </div>

    <Drawer :width="320" v-model:visible="hotkeyDrawerVisible" placement="right">
      <HotkeyDoc />
    </Drawer>
    <Modal :width="1200" closeButton :visible="helpVisible" @closed="helpVisible = false">
      <img width="1100" src="./images/ppt_show.png" alt="">
    </Modal>
    <FullscreenSpin :loading="exporting" :tip="tip" />
  </div>
</template>

<script lang="ts" setup>
import { nextTick, ref, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore, useSlidesStore } from '@/components/PPTist/store'
import useScreening from '@/components/PPTist/hooks/useScreening'
import useImport from '@/components/PPTist/hooks/useImport'
import useSlideHandler from '@/components/PPTist/hooks/useSlideHandler'
import type { DialogForExportTypes } from '@/components/PPTist/types/export'

import HotkeyDoc from './HotkeyDoc.vue'
import FileInput from '@/components/PPTist/components/FileInput.vue'
import FullscreenSpin from '@/components/PPTist/components/FullscreenSpin.vue'
import Drawer from '@/components/PPTist/components/Drawer.vue'
import Input from '@/components/PPTist/components/Input.vue'
import Popover from '@/components/PPTist/components/Popover.vue'
import PopoverMenuItem from '@/components/PPTist/components/PopoverMenuItem.vue'
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const mainStore = useMainStore()
const slidesStore = useSlidesStore()
const { title } = storeToRefs(slidesStore)
const { enterScreening, enterScreeningFromStart } = useScreening()
const { importSpecificFile, importPPTXFile, exporting } = useImport()
const { resetSlides } = useSlideHandler()
const tip = computed(() => exporting ? '正在导入...' : '加载中...')

const mainMenuVisible = ref(false)
const hotkeyDrawerVisible = ref(false)
const editingTitle = ref(false)
const titleInputRef = ref<InstanceType<typeof Input>>()
const titleValue = ref('')
const helpVisible = ref(false)
const startEditTitle = () => {
  titleValue.value = title.value
  editingTitle.value = true
  nextTick(() => titleInputRef.value?.focus())
}

const handleUpdateTitle = () => {
  slidesStore.setTitle(titleValue.value)
  editingTitle.value = false
}

const goLink = (url: string) => {
  window.open(url)
  mainMenuVisible.value = false
}

const setDialogForExport = (type: DialogForExportTypes) => {
  mainStore.setDialogForExport(type)
  mainMenuVisible.value = false
}
// 显示关联报价单dialog
const linkDialogForQuote = () => {
  mainMenuVisible.value = false
  proxy.$emit('show')
}
// 保存模板
const saveModule = () => {
  mainMenuVisible.value = false
  proxy.$emit('saveModule')
}

</script>

<style lang="scss" scoped>
.editor-header {
  background-color: #fff;
  user-select: none;
  border-bottom: 1px solid $borderColor;
  display: flex;
  justify-content: space-between;
  padding: 0 5px;
}

.left,
.right {
  display: flex;
  justify-content: center;
  align-items: center;
}

.menu-item {
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  padding: 0 10px;
  border-radius: $borderRadius;
  cursor: pointer;

  .icon {
    font-size: 18px;
    color: #666;
  }

  &:hover {
    background-color: #f1f1f1;
  }
}

.group-menu-item {
  height: 30px;
  display: flex;
  margin: 0 8px;
  padding: 0 2px;
  border-radius: $borderRadius;

  &:hover {
    background-color: #f1f1f1;
  }

  .menu-item {
    padding: 0 3px;
  }

  .arrow-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
}

.title {
  height: 32px;
  margin-left: 2px;
  font-size: 13px;

  .title-input {
    width: 200px;
    height: 100%;
    padding-left: 0;
    padding-right: 0;
  }

  .title-text {
    min-width: 20px;
    max-width: 400px;
    line-height: 32px;
    padding: 0 6px;
    border-radius: $borderRadius;
    cursor: pointer;

    @include ellipsis-oneline();

    &:hover {
      background-color: #f1f1f1;
    }
  }
}

.github-link {
  display: inline-block;
  height: 30px;
}

.help {
  margin-left: 20px;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  min-width: 20px;
  max-width: 400px;
  padding: 0 6px;
  border-radius: 2px;
  cursor: pointer;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-left: 2px;
  font-size: 13px;
  padding: 4px 10px;
  color: #fff;
  background-color: #409eff;

  .icon {
    cursor: pointer;
  }
}
</style>